﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ObTookit.Auto
{
    public class SQLCoder:BaseCoder
    {
        public string CreateCode()
        {
            string text = ReadTemplate(Template);
            text = text.Replace("[#AUTHOR#]", Author);
            text = text.Replace("[#CREATED#]", CreateDate);

            text = text.Replace("[#DbName#]", AutoObj.DbName);
            text = text.Replace("[#TbName#]", AutoObj.TbName);
            text = text.Replace("[#CreateTbFields#]", CreateSQL());
            text = text.Replace("[#CreateTbExtProp#]", CreateExtProperty());

            text = text.Replace("[#TbFields#]", CreateTbFields());
            //
            text = text.Replace("[#UpdateTbFields#]", CreateUpdateTbFields());
            return text;
        }

        public string CreateSQL()
        {
            StringBuilder sb = new StringBuilder();
            foreach (FieldObject fo in FieldObjs)
            {
                sb.Append(CodeArgs.TAB + "[" + fo.Name + "] [" + fo.DbType + "]");
                if (!fo.PrimaryKey)
                {
                    if ("VARCHAR".Equals(fo.DbType.ToUpper()))
                    {
                        sb.Append(" ("+fo.Length+") NOT NULL," 
                            + CodeArgs.RN);
                    }
                    else
                    {
                        sb.Append(" NOT NULL," 
                            + CodeArgs.RN);
                    }
                }
                else
                {
                    if (fo.Identity)
                    {
                        sb.Append(" PRIMARY KEY IDENTITY(1,1) NOT NULL,"
                       + CodeArgs.RN);
                    }
                    else
                    {
                        sb.Append(" PRIMARY KEY NOT NULL,"
                       + CodeArgs.RN);
                    }
                }
            }
            sb.Append("GO" + CodeArgs.RN);
            return sb.ToString();
        }

        public string CreateExtProperty()
        {
            StringBuilder sb = new StringBuilder();
            foreach (FieldObject fo in FieldObjs)
            {
                sb.Append(CodeArgs.TAB + "EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'" + fo.Descr + "' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'" + AutoObj.TbName + "', @level2type=N'COLUMN',@level2name=N'" + fo.Name + "'"
                    + CodeArgs.RN);
                sb.Append("GO" + CodeArgs.RN);
            }
            sb.Append(CodeArgs.TAB + "EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'"
                + AutoObj.TbDescri +"' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'"
                + AutoObj.TbName +"'"
                + CodeArgs.RN);
            sb.Append("GO" + CodeArgs.RN);
            return sb.ToString();
        }

        public string CreateTbFields()
        {
            string fields = "";
            foreach (FieldObject fo in FieldObjs)
            {
                fields += "[" + fo.Name + "]" + CodeArgs.RN + CodeArgs.TAB + ",";
            }
            return fields.TrimEnd(',');
        }

        public string CreateUpdateTbFields()
        {
            string fields = "";
            foreach (FieldObject fo in FieldObjs)
            {
                fields += "[" + fo.Name + "] = " + CodeArgs.RN + CodeArgs.TAB + ",";
            }
            return fields.TrimEnd(',');
        }

    }
}
